#ifndef DEFINE_H
#define DEFINE_H

#define true        1
#define false       0


#define BACKLIGHT_WEAK_TIME       (30)
#define HEARTBEAT_TIMEOUT         (20)
#define RTC_ACK_TIME              (10)

typedef unsigned int     uint32_t;
typedef unsigned short   uint16_t;
typedef unsigned char     uint8_t;
typedef signed int        int32_t;
typedef signed short      int16_t;
typedef signed char        int8_t;

typedef enum
{
    SYS_RUN,
    SYS_STOP,
    SYS_PWRDOWN,
}System_status_t;

typedef enum
{
    Micro_No_Trigger,
    Micro_May_Trigger,
    Micro_Trigger,
}mcicro_sta_t;

typedef enum
{
    UART_RX_IDLE,
    UART_RX_RECVING,
    UART_RX_DONE,
}uart_flag_t;

typedef enum
{
	SETADDR = 0xAB,
	SETADDR_ACK = 0xAA,
	RTNERR = 0xA7,
	SETTMP = 0xA4,
	// RTNTMP = 0xA3,
	HEARTBEAT = 0xA1,
	HEARTBEAT_ACK = 0xA3,
}Header;

typedef enum
{
    MACH_NORMAL,
    MACH_CONFIG,
    MACH_AUTO,
    MACH_MANUAL,
}machine_t;

typedef struct 
{
    uint8_t wakeup;
    uint8_t system_state;
    uint8_t mach_state;
    uint8_t micro_flag;
    uint8_t uart1_rx_flag;
    uint8_t connect_timeout;
    uint8_t mode;
    uint8_t rtc_hf_ack_time;
    volatile uint8_t sleeping;
    uint16_t micro_timeout;
    uint16_t Env_Temp;
    uint16_t Target_Temp;
    uint16_t uart_timeout;
    uint16_t weak_time;
    uint8_t lora_tx_buf[64];
    float battery_voltage;
}panel_t;


typedef struct _addr_t
{
	uint8_t haddr;
    uint8_t laddr;
	uint8_t CH;
}loraAddr_t;

#define BIT0         						0x01
#define BIT1         						0x02
#define BIT2         						0x04
#define BIT3         						0x08
#define BIT4         						0x10
#define BIT5         						0x20
#define BIT6         						0x40
#define BIT7         						0x80
#define BIT8         						0x100
#define BIT9         						0x200
#define BIT10         					0x400
#define BIT11         					0x800
#define BIT12         					0x1000
#define BIT13         					0x2000
#define BIT14         					0x4000
#define BIT15         					0x8000


#define __NOP1__  	_nop_()
#define __NOP2__ 	__NOP1__; __NOP1__
#define __NOP4__ 	__NOP2__; __NOP2__
#define __NOP8__ 	__NOP4__; __NOP4__
#define __NOP16__ 	__NOP8__; __NOP8__
#define __NOP32__ 	__NOP16__; __NOP16__
#define __NOP64__ 	__NOP32__; __NOP32__
#define __NOP128__ 	__NOP64__; __NOP64__


#define DelayNop(a)             						\
    if ((a)&(0x01))    {__NOP1__;}                    \
    if ((a)&(0x02))    {__NOP2__;}                    \
    if ((a)&(0x04))    {__NOP4__;}                    \
    if ((a)&(0x08))    {__NOP8__;}                    \
    if ((a)&(0x10))    {__NOP16__;}                    \
    if ((a)&(0x20))    {__NOP32__;}                    \
    if ((a)&(0x40))    {__NOP64__;}                    \
    if ((a)&(0x80))    {__NOP128__;}          
	
extern panel_t xdata stupanel;

void sys_stop(void);
void sys_down(void);
void sys_restart(void);
unsigned short usMBCRC16(unsigned char *pu8Buf, unsigned char u8Len);
#endif										
